#include "threadpool.h"

int deal_message(int* netfd,threadPool_t* pool,char*user,DirStack* stack,MYSQL* conn){
    train_t train;
    char path[] = "../users";
    char result_dir[1024] ={0};
    memset(&train, 0, sizeof(train_t));
    int ret = recv(*netfd, &train.command,sizeof(train.command),0);
    if(ret == 0){
        close(*netfd);
        *netfd = -1;
        return -1;
    }
    printf("train.command = %d\n",train.command);
    recv(*netfd, &train.length,sizeof(train.length),0);
    printf("train.length = %d\n",train.length);
    if(train.length != 0){
        recv(*netfd,train.data,train.length,0);
        printf("train.data = %s\n",train.data);
    }
    switch(train.command){
    case CD:{
                ret = cd_command(conn,stack,train.data,user);
                if(ret != 0){
                    train.command = ERROR_COMMAND;
                    memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
                    train.length = strlen(train.data);
                    send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                };
                break;
            }
    case PWD:{
                 pwd_command(conn,stack,user,result_dir);
                 train.command = PWD;
                 train.length = strlen(result_dir);
                 memcpy(train.data,result_dir,train.length);
                 send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                 break;
             }
    case LS: {
                 ls_command(conn,stack,user,result_dir);
                 train.command = LS;
                 train.length = strlen(result_dir);
                 memcpy(train.data,result_dir,sizeof(train.data));
                 send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                 break;
             }
    case MKDIR:{ 
                   ret = mkdir_command(conn,stack,train.data,user);
                   if(ret != 0){
                       train.command = ERROR_COMMAND;
                       memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
                       train.length = strlen(train.data);
                       send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                   }
                   break;
               }
    case RMDIR:{
                   ret = rm_command(conn,train.data,stack,user);
                   if(ret != 0){
                       train.command = ERROR_COMMAND;
                       memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
                       train.length = strlen(train.data);
                       send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);

                   }
                   break;
               }
    case PUTS:{
                  break;
              }
    case GETS:{
                  putsCommand(*netfd,conn,path,train.data); 
                  break;
              }
    case REMOVE:{
                    ret = rm_command(conn,train.data,stack,user);
                    if(ret != 0){
                        train.command = ERROR_COMMAND;
                        memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
                        train.length = strlen(train.data);
                        send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                    }
                    break;
                }
    case QUIT:{ 
                  close(*netfd);*netfd = -1;
                  break;
              }
    case ERROR_COMMAND:
              break;
    default:break;
    }
    return 0;
}



